package com.cskaoyan.controller.wx.lghc;

import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.vo.VxShopCartCheckoutVO;
import com.cskaoyan.module.bean.vo.VxShopCartIndexVO;
import com.cskaoyan.module.bean.vo.VxShopCartInnerIndexVO;
import com.cskaoyan.module.bo.VxShopCartAddBO;
import com.cskaoyan.module.bo.VxShopCartFastAddBO;
import com.cskaoyan.module.bo.VxShopCartUpdateBO;
import com.cskaoyan.module.bo.VxShopcartCheckedOutBO;
import com.cskaoyan.module.pojo.MarketCart;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.service.wx.lghc.VxShopCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/9  15:22
 */
@RestController
@RequestMapping("wx/cart")
public class VxShopCartController {

    @Autowired
    VxShopCartService shopCartService;


    @RequestMapping("index")
    public BaseRespVo indexShopCart(VxShopCartIndexVO vxShopCartIndexVO, MarketCart marketCart, VxShopCartInnerIndexVO vxShopCartInnerIndexVO){

        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject.isAuthenticated());
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = marketUser.getId();
       VxShopCartIndexVO vxShopCartIndexVO1 =  shopCartService.indexShopCart(vxShopCartIndexVO,marketCart,vxShopCartInnerIndexVO,id);

        return BaseRespVo.ok(vxShopCartIndexVO1);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount(){

       Integer reslut =  shopCartService.goodsCount();

       return BaseRespVo.ok(reslut);
    }

    @PostMapping("checked")

    public BaseRespVo goodsChecked(@RequestBody Map map){

        List<Integer> productIds = (List<Integer>) map.get("productIds");
        Integer isChecked = (Integer) map.get("isChecked");
        VxShopCartIndexVO vxShopCartIndexVO1 =shopCartService.goodsChech(productIds,isChecked);
        VxShopCartInnerIndexVO cartTotal = vxShopCartIndexVO1.getCartTotal();
        Double checkedGoodsAmount = cartTotal.getCheckedGoodsAmount();
        VxShopCartInnerIndexVO cartTotal1 = vxShopCartIndexVO1.getCartTotal();
        Integer checkedGoodsCount = cartTotal1 .getCheckedGoodsCount();
        if(checkedGoodsAmount == null){
            checkedGoodsAmount =0.0;
            cartTotal.setCheckedGoodsAmount(checkedGoodsAmount);

        }

        if(checkedGoodsCount ==null){
            checkedGoodsCount =0;
            cartTotal1.setCheckedGoodsCount(checkedGoodsCount);
        }

        return BaseRespVo.ok(vxShopCartIndexVO1);
    }

    @PostMapping("update")
    public BaseRespVo shopCartUpdate(@RequestBody VxShopCartUpdateBO shopCartUpdateBO){

        shopCartService.goodsUpdate(shopCartUpdateBO);
        return BaseRespVo.ok("成功");

    }

    @PostMapping("add")
    public BaseRespVo shopCartAdd(@RequestBody VxShopCartAddBO vxShopCartAddBO){

        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = marketUser.getId();

        Integer result =  shopCartService.shopCartAdd(vxShopCartAddBO,id);

        return BaseRespVo.ok(result);
    }

    @PostMapping("delete")
    public BaseRespVo deleteShopCart(@RequestBody Map map){

        List<Integer> productIds = (List<Integer>) map.get("productIds");

        VxShopCartIndexVO vxShopCartIndexVO = shopCartService.deleteShopCart(productIds);
        return BaseRespVo.ok(vxShopCartIndexVO);
    }

    @RequestMapping("checkout")
    public BaseRespVo checkOutShopCart( VxShopcartCheckedOutBO vxShopcartCheckedOutBO){

        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject.isAuthenticated());
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = marketUser.getId();

        VxShopCartCheckoutVO vxShopCartCheckoutVO = shopCartService.checkedOut(vxShopcartCheckedOutBO,id);

        return BaseRespVo.ok(vxShopCartCheckoutVO);

    }

    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody VxShopCartFastAddBO vxShopCartFastAddBO){

        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = marketUser.getId();
       int resultFastAdd = shopCartService.fastAdd(vxShopCartFastAddBO,id);


        return BaseRespVo.ok(resultFastAdd);
    }
}
